package xyz.longicorn.driver.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import xyz.longicorn.driver.pojo.FileInfo;
import xyz.longicorn.driver.pojo.FolderInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 文件项 可以是文件也可以是目录
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class FileItem {
    private Long id;
    private String name;
    private Long size;
    private String path;
    private String thumb;
    private Date createTime;
    private Date updateTime;
    private String type;

    public static List<FileItem> buildFolderList(List<FolderInfo> list) {
        List<FileItem> listItems = new ArrayList<>();
        list.forEach(f -> listItems.add(build(f)));
        return listItems;
    }

    public static FileItem build(FolderInfo f) {
        return new FileItem()
                .setCreateTime(f.getCreateTime())
                .setId(f.getId())
                .setName(f.getName())
                .setSize(0l)
                .setType("folder")
                .setPath(f.getPath())
                .setUpdateTime(f.getUpdateTime());
    }

    public static List<FileItem> buildFileList(List<FileInfo> list) {
        List<FileItem> listItems = new ArrayList<>();
        list.forEach(f -> listItems.add(build(f)));
        return listItems;
    }

    /**
     * 将文件转成fileItem
     *
     * @param f
     * @return
     */
    public static FileItem build(FileInfo f) {
        return new FileItem()
                .setCreateTime(f.getCreateTime())
                .setId(f.getId())
                .setName(f.getName())
                .setSize(f.getSize())
                .setType(f.getType())
//                .setPath(f.getPath())
//                .setThumb(f.getPath())
                .setPath("http://localhost:8080/picture/preview?hash=" + f.getHash())
                .setThumb("http://localhost:8080/picture/thumb?hash=" + f.getHash())
                .setUpdateTime(f.getUpdateTime());
    }
}
